package ar.Location.Api.GooglePlace;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ar.Location.PlaceObject.Place;

public class GooglePlaceDetailHandler extends DefaultHandler {

	boolean is_entered_result_TAG = false;
	public String categoryName = "";

	GooglePlace gPlace = null;
	StringBuffer stringBuffer = new StringBuffer();

	public GooglePlaceDetailHandler(String name) {
		categoryName = name;
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub

		stringBuffer.setLength(0);

		if (localName.equals("result")) {
			is_entered_result_TAG = true;
			gPlace = new GooglePlace(categoryName);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		stringBuffer.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		String innerText = stringBuffer.toString().trim();

		if (is_entered_result_TAG) {
			if (localName.equals("formatted_phone_number")) {
				gPlace.phoneNumber = innerText;
			}
			if (localName.equals("formatted_address")) {
				gPlace.address = innerText;
			}
			if (localName.equals("url")) {
				gPlace.websiteUrl = innerText;
			}
		}

		if (localName.equals("result")) {
			is_entered_result_TAG = false;
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	public Place getPlaceDetail() {
		return gPlace;
	}
}
